package com.bw.module_attendance.viewmodel


import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.coroutine.CoroutineHelper
import com.bw.lib_common.vm.BaseViewModel2
import com.bw.module_attendance.entity.AttendanceEntity
import com.bw.module_attendance.repo.AttendanceRepo
import kotlinx.coroutines.launch

/**
 * 工单-智慧园区-考勤管理
 */
class AttendanceViewModel : BaseViewModel2<AttendanceRepo>() {
    val attendanceSuccess by lazy { MutableLiveData<List<AttendanceEntity>>() }

    fun getAttendance(userId:Int){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getAttendance(userId)
            if (result.code == 200){
                attendanceSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue("数据异常！！")
            }
        }
    }


    val addAttendanceSuccess by lazy { MutableLiveData<AttendanceEntity>() }


    fun addAttendance(attendanceEntity: AttendanceEntity){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.addAttendance(attendanceEntity)
            if (result.code == 200){
                addAttendanceSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue("数据异常！！")
            }
        }
    }

    override fun createRepository(): AttendanceRepo = AttendanceRepo()


}